package com.example.administrator.libraryclub;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.Toast;

import com.example.administrator.libraryclub.fragment.FirstFragment;
import com.example.administrator.libraryclub.fragment.SecondFragment;

/**
 * Created by Administrator on 2015/10/21.
 */
public class AnimatorActivity extends Activity {

    private FirstFragment firstFragment;
    private SecondFragment secondFragment;

    private Fragment curFragment;

    private GestureDetector gestureDetector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.animator_activity);

        setDefaultFragment();

        setGestureDetector();

    }

    private void setGestureDetector() {
        gestureDetector = new GestureDetector(this, new GestureDetector.OnGestureListener() {
            @Override
            public boolean onDown(MotionEvent motionEvent) {
                return true;
            }

            @Override
            public void onShowPress(MotionEvent motionEvent) {

            }

            @Override
            public boolean onSingleTapUp(MotionEvent motionEvent) {
                return true;
            }

            @Override
            public boolean onScroll(MotionEvent e, MotionEvent e1, float v, float v1) {
                return false;
            }

            @Override
            public void onLongPress(MotionEvent motionEvent) {

            }

            @Override
            public boolean onFling(MotionEvent e, MotionEvent e1, float v, float v1) {
                //向下滑动
                if (e.getRawY() - e1.getRawY() < 100){
                    if(curFragment != secondFragment)
                    setFragment(secondFragment);
                }
                //向上滑动
                if (e1.getRawY() - e.getRawY() < 100){
                    if(curFragment != firstFragment)
                    setFragment(firstFragment);
                }
                return true;
            }
        });
    }

    private void setFragment(Fragment fragment) {
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.setCustomAnimations(R.animator.fragment_top_enter,R.animator.fragment_top_exit,R.animator.fragment_pop_top_enter,R.animator.fragment_pop_top_exit);
        transaction.replace(R.id.fl_content,fragment);
        transaction.commit();
        curFragment = fragment;
    }

    private void setDefaultFragment() {
        firstFragment = new FirstFragment();
        secondFragment = new SecondFragment();
        setFragment(firstFragment);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Toast.makeText(this, "测试Activity的onTouchEvent事件", Toast.LENGTH_LONG
        ).show();
        return super.onTouchEvent(event);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        Toast.makeText(this, "测试dispatchTouchEvent事件", Toast.LENGTH_LONG
        ).show();
        gestureDetector.onTouchEvent(ev);
        return super.dispatchTouchEvent(ev);
    }
}
